package ATBMS.control;

import ATBMS.modles.DAO.*;
import ATBMS.modles.Flight;
import ATBMS.view.manager.ManagerViewInterface;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * Manager CoControllerntrol
 */
public class ManagerControl {

    private ManagerViewInterface mv;

    /**
     * Set Manager View and show flights
     * @param Adminstrator View Interface
     */
    public void setManagerView(ManagerViewInterface mv) {
        this.mv = mv;

        refreshFlightTable();
    }

    /**
     * Show All Flights
     */
    private void refreshFlightTable() {
        FlightDAO flightdao = DAOFactory.getFlightDAO();

        if (flightdao == null) {
            mv.alert("Can't Connect To DB!");
            return;
        }
        List<Flight> flights = flightdao.findAll();
        mv.listFlight(flights);
    }

    /**
     * Creates a new <code>ManagerControl</code> object
     */
    public ManagerControl() {
    }

    /**
     * Create a Action For Manager View's Delete Flight Menu Item
     * @return Action
     */
    public javax.swing.Action getDelFlightAction() {
        return new javax.swing.AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                String flightCode = mv.getSelectedFlightCode();
                if (!flightCode.isEmpty()) {
                    FlightDAO flightdao = DAOFactory.getFlightDAO();
                    if (flightdao == null) {
                        mv.alert("Can't Connect To DB!");
                        return;
                    }

                    Flight flight = flightdao.findByFlightCode(flightCode);
                    if (flight != null) {
                        flightdao.delete(flight.getFid());
                        refreshFlightTable();
                    } else {
                        mv.alert("Please select a flight");
                    }
                } else {
                    mv.alert("Please select a flight");
                }
            }
        };
    }

    /**
     * Create a Action For Manager View's Edit Flight Menu Item
     * @return Action
     */
    public javax.swing.Action getEditFlightAction() {
        return new javax.swing.AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                String flightCode = mv.getSelectedFlightCode();
                if (!flightCode.isEmpty()) {
                    FlightDAO flightdao = DAOFactory.getFlightDAO();
                    if (flightdao == null) {
                        mv.alert("Can't Connect To DB!");
                        return;
                    }

                    Flight flight = flightdao.findByFlightCode(flightCode);
                    if (flight != null) {
                        if ((flight = mv.showEditFlightDialog(flight)) != null) {
                            if (flightdao.update(flight.getFid(), flight)) {
                                refreshFlightTable();
                            }
                        }
                    } else {
                        mv.alert("Please select a flight");
                    }
                } else {
                    mv.alert("Please select a flight");
                }
            }
        };
    }

    /**
     * Create a Action For Manager View's New Flight Menu Item
     * @return Action
     */
    public javax.swing.Action getNewFlightAction() {
        return new javax.swing.AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                Flight newFlight = mv.showNewFlightDialog();
                if (newFlight != null) {
                    FlightDAO flightdao = DAOFactory.getFlightDAO();
                    if (flightdao == null) {
                        mv.alert("Can't Connect To DB!");
                        return;
                    }

                    if (flightdao.insert(newFlight)) {
                        refreshFlightTable();
                    }

                }
            }
        };
    }
}
